var utils = (function () {
  var isIe678 = /MSIE (6|7|8)/i.test(navigator.userAgent);
  function getEle(attrName) {
    var reg = /^(#)|(\.)|(\w{1,})/g;
    var res = reg.exec(attrName);
    if (res && res.length) {
      switch (res[0]) {
        case '#':
          return document.getElementById(attrName.match(reg)[1]);
        case '.':
          return document.getElementsByClassName(attrName.match(reg)[1]);
        default:
          return document.getElementsByTagName(attrName);
      }
    }
    return null;
  };
  function getChildren(targetEle, tagname) {
    var childrens = [];
    if (/MSIE (6|7|8)/i.test(navigator.userAgent)) {
      var nodes = targetEle.childNodes;
      for (let i = 0; i < nodes.length; i++) {
        if (nodes[i].nodeType === 1) {
          childrens[childrens.length] = nodes[i];
        }
      };
    } else {
      childrens = this.listToArray(targetEle.children);
      // Array.prototype.slice.call(targetEle.children);
    };
    if (typeof tagname === 'string') {
      for (let j = 0; j < childrens.length; j++) {
        if (childrens[j].nodeName.toLowerCase() !== tagname.toLowerCase()) {
          childrens.splice(j, 1);
          j--;
        }
      };
    }
    return childrens;
  };
  function getPrev(targetEle){
    if (isIe678) return targetEle.previousElementSibling;
    var prev = targetEle.previousSibling;
    while (prev && prev.nodeType !== 1) {
      prev = prev.previousSibling;
    }
    return prev;
  };
  function getPrevAll(targetEle){
    var arr = [];
    var prev = getPrev(targetEle);
    while (prev) {
      arr.unshift(prev);
      prev = getPrev(prev)
    }
    return arr;
  };
  function getNextAll(targetEle){
    var arr = [];
    var next = this.next(targetEle);
    while (next) {
      arr.push(next);
      next = this.next(next)
    }
    return arr;
  };
  function getNext(targetEle){
    if (isIe678) return targetEle.nextElementSibling;
    var next = targetEle.nextSibling;
    while (next && next.nodeType !== 1) {
      next = next.nextSibling;
    }
    return next;
  };
  function getSiblings(targetEle) {
    return this.prevAll(targetEle).concat(this.nextAll(targetEle));
  };
  function getFirstChild(targetEle) {
    if (this.children(targetEle).length > 0) return this.children(targetEle)[0];
  };
  function getLastChild(targetEle) {
    if (this.children(targetEle).length > 0) return this.children(targetEle)[this.children(targetEle).length - 1];
  };
  // 目标元素中后面插入新元素
  function append(targetEle, newEle) {
    targetEle.appendChild(newEle);
  }
  // 向指定元素开头添加新元素
  function prepend(targetEle, newEle) {
    const first = this.firstChild(targetEle);
    if (first) {
      targetEle.insertBefore(newEle, first);
      return;
    }
    targetEle.appendChild(newEle)
  }
  // 添加到指定元素前面
  function insertBefore(targetEle, newEle) {
    targetEle.parentNode.insertBefore(newEle, targetEle);
  }
  // 添加到指定元素后面
  function insertAfter(targetEle, newEle) {
    var next = this.next(targetEle);
    if (next) {
      targetEle.parentNode.insertBefore(newEle, next);
      return;
    }
    targetEle.parentNode.appendChild(newEle);
  }
  function addClass(targetEle, newClass) {
    var classList = targetEle.className;
    if (newClass) {
      if (classList) {
        var arr = classList.split(' ');
        if (arr.indexOf(newClass) === -1) {
          arr.push(newClass);
          targetEle.className = arr.join(' ');
        }
      } else {
        targetEle.className = newClass;
      }
    }
  }
  function removeClass(targetEle, newClass) {
    var classList = targetEle.className;
    if (newClass) {
      if (classList) {
        var reg = new RegExp("(^|\\s)" + newClass + "(\\s|$)");
        targetEle.className = classList.replace(reg, ' ')
      } else {
        targetEle.className = '';
      }
    }
  }



  return {
    // 类数组转换成数组
    listToArray: function (likeArray) {
      var arr = [];
      try {
        // call 是改变this上下文， 传参方式为*.call(thisObj, pramas1, pramas2, ...)
        arr = Array.prototype.slice.call(likeArray);
      } catch (e) {
        for (var i = 0; i < likeArray.length; i++) {
          arr[arr.length] = likeArray[i];
        }
      }
      return arr;
    },
    // 数组合并
    combineArr: function (arr1, arr2) {
      Array.prototype.push.apply(arr1, arr2);
      return arr1;
    },
    // 数组的平均数 参传入
    average: function () {
      var args = this.listToArray(arguments);
      if (args[0] instanceof Array) {
        return (eval(args[0].join('+')) / args[0].length);
      }
      return (eval(args.join('+')) / args.length);
    },
    // 请数组最大值
    max: function (arry) {
      return Math.max.apply(null, arry);
    },
    min: function (arry) {
      return Math.min.apply(null, arry);
    },
    // 排序
    sortAs: function (arr) {
      arr.sort(function (a, b) {
        return a - b;
      })
      return arr;
    },
    sortDes: function (arr) {
      arr.sort(function (a, b) {
        return b - a;
      })
      return arr;
    },
    // 把数组的方向调用
    reverse: function (arr) {
      arr.sort(function (a, b) {
        return 1; // 每次a, b都会调换位置。
      })
      return arr;
    },
    // 千分位 n表示保留小点位数， 可以不填
    toThousands: function (s, n) {
      n = n > 0 && n <= 20 ? n : 0;
      s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
      var l = s.split('.')[0].split('').reverse();
      var r = n > 0 ? s.split('.')[1] : '';
      var t = '';
      for (var i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '');
      }
      return n > 0 ? (t.split('').reverse().join('') + '.' + r) : t.split('').reverse().join('');
    },
    // 时间格式化
    _formatDate: function () {
      var str = '2017/04/05 08:37:34';
      var reg = /^(\d{4})[/-](\d{1,2})[/-](\d{1,2}) +(\d{1,2})[:](\d{1,2})[:](\d{1,2})$/g;
      var arr = [];
      arr = reg.exec(str).slice(1, 7);
      // 时间格式
      var res = arguments[0] || '{0}年{1}月{2}日{3}时{4}分{5}秒';
      res = res.replace(/{(\d+)}/g, function () {
        var num = arguments[1];
        var val = arr[num];
        return val;
      })
      return res;
    },
    // 获取浏览器可视区域
    getWin: function (attr, val) {
      if (arguments.length > 1) {
        document.documentElement[attr] = val;
        document.body[attr] = val;
      }
      return document.documentElement[attr] || document.body[attr]
    },
    //获取到某个节点的style或具体的某一个属性； attr参数可选
    getCss: function (curEle, attr) {
      if (arguments.length > 1) {
        if (/MSIE (?:6|7|8)/.test(window.navigator.userAgent)) {
          return curEle.currentStyle[attr];
        }
        return window.getComputedStyle(curEle, null)[attr];
      } else {
        if (window.getComputedStyle) {
          return window.getComputedStyle(curEle, null);
        }
        return curEle.currentStyle;
      }
    },
    // 数组去重
    unique: function (array) {
      
    },
    // 判断是否为IE
    isIE: function () {
      if (!!window.ActiveXObject) {
        return true;
      } else {
        return false;
      }
    },
    // 判断是IE那个版本
    isIENUM: function () {
      var isIE = !!window.ActiveXObject;
      var isIE6 = isIE && !window.XMLHttpRequest;
      var isIE8 = isIE && !!document.documentMode;
      var isIE7 = isIE && !isIE6 && !isIE8;
      if (isIE) {
        if (isIE6) {
          return 'ie6';
        } else if (isIE8) {
          return 'ie8';
        } else if (isIE7) {
          return 'ie7';
        }
      } else {
        return false
      }
    },
    // 可以循环数组和对象的方法
    each: function (obj, fn) {
      if (obj instanceof Array) {
        obj.forEach(function (item, index) {
          fn(item, index);
        })
      } else {
        for (key in obj) {
          fn(obj[key], key);
        }
      }
    },
    // format 时间戳 data 是传new Date()   str传分隔符如- /等
    format: function (date, str) {
      var arr = [];
      var connect;
      arr.push(date.getFullYear());
      arr.push((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1));
      arr.push((date.getDate() + 1) < 10 ? '0' + (date.getDate() + 1) : (date.getDate() + 1));
      if (str.indexOf('/') >= 0) {
        connect = '/';
      } else if (str.indexOf('-') >= 0) {
        connect = '-';
      }
      return arr.join(connect);
    },
    // 获取或设置浏览器滚动条
    winScroll: function (attr, value) {
      if (value === 'undefined') {
        return document.documentElement[attr] || document.body[attr];
      }
      document.documentElement[attr] = value;
      document.body[attr] = value;
    },
    getCss: function (ele, attr, value) {
      if (value === undefined) {
        if (window.getComputedStyle) {
          return parseFloat(window.getComputedStyle(ele, null)[attr]);
        }
        return parseFloat(ele.currentStyle[attr]);
      } else {
        ele.style[attr] = value;
      }
    },
    // 获取任何元素的相对浏览器左侧和上边的距离；
    getOffset: function (ele) {
      var eleLeft = ele.offsetLeft;
      var eleTop = ele.offsetTop;
      var eleParent = ele.offsetParent;
      var left = null;
      var top = null;
      left += eleLeft;
      top += eleTop;
      while (eleParent) {
        left += eleParent.clientLeft + eleParent.offsetLeft;
        top += eleParent.clientTop + eleParent.offsetTop;
        eleParent = eleParent.offsetParent;
      }
      return { left: left, top: top };
    },
    $: getEle,
    // 获取DOM 子节点节点
    children: getChildren,
    prev: getPrev,
    next: getNext,
    prevAll: getPrevAll,
    nextAll: getNextAll,
    siblings: getSiblings,
    firstChild: getFirstChild,
    lastChild: getLastChild,
    prepend: prepend,
    append: append,
    insertAfter: insertAfter,
    insertBefore: insertBefore,
    addClass: addClass,
    removeClass: removeClass,
    addClass: addClass,
  };
})();